package com.belencarrasco.serviciopedidoscliente;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.belencarrasco.encrip.Encripter;
import com.belencarrasco.serviciopedidoscliente.ListasActivity.ObtenerPedidosBackground;
import com.google.gson.JsonObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author Belen
 *
 */
public class AddPedidoActivity extends Activity{
	
	
	private String URL = Constantes.HOST + "/ServicioPedidosSoap/services/ServicioPedidosORMWS";
	private String NAMESPACE = "http://ws.orm.belencarrasco.com";
	private EditText mesa;
	private EditText descripcion;
	
	 /* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_agregar_pedido);
	        
	        mesa = (EditText) findViewById(R.id.editMesa);
	        descripcion = (EditText) findViewById(R.id.editDescripcion);
	    }
	 
		/**
		 * Metodo que da funcionalidad al boton que agrega pedidos en la interfaz android
		 * Y encripta el contenido
		 * @param v
		 */
		public void agregarPed(View v){
			
			String encrip = "";
			
			JsonObject jsonObjectPedido = new JsonObject();
			jsonObjectPedido.addProperty("mesa", mesa.getText().toString());
			jsonObjectPedido.addProperty("descripcion", descripcion.getText().toString());
			jsonObjectPedido.addProperty("estado", 0);
			
			// encriptar 
			encrip = Encripter.encripter(jsonObjectPedido.toString());
			
			new AddPedidoBackground().execute(encrip);			
			
		}
		
		class AddPedidoBackground extends AsyncTask<String, Void, String>{
			
			private ProgressDialog progressDialog;

			/* (non-Javadoc)
			 * @see android.os.AsyncTask#onPreExecute()
			 */
			@Override
			protected void onPreExecute() {
	    		progressDialog = ProgressDialog.show(
	    				AddPedidoActivity.this,
	    				"Pedidos", 
	    				"Espere mientras se agrega el pedido...", 
	    				true);
				super.onPreExecute();
			}
			
			/* (non-Javadoc)
			 * @see android.os.AsyncTask#doInBackground(Params[])
			 */
			@Override
			protected String doInBackground(String... params) {

				String resultado = new String();
				String METHOD_NAME = "addPedido";
				String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				soapEnvelope.dotNet = true;
				soapEnvelope.implicitTypes = true;
				soapEnvelope.setOutputSoapObject(request);

				request.addProperty("pedidoJson", params[0]);

				HttpTransportSE aht = new HttpTransportSE(URL);

				try {
					aht.call(SOAP_ACTION, soapEnvelope);
					SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
					resultado = resultString.toString();

				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				}

				return resultado;
			}

			/* (non-Javadoc)
			 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
			 */
			@Override
			protected void onPostExecute(String result) {
				super.onPostExecute(result);
				progressDialog.dismiss();
				//Valido que tenga la informacion necesaria
				if("1".equals(result)){
					Toast.makeText(AddPedidoActivity.this, "Ingresada correctamente", Toast.LENGTH_LONG).show();
					finish();
				}else{
					Toast.makeText(AddPedidoActivity.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
				}
			}
		}

}
